package com.wangjialu.learner.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wangjialu.learner.consumer.service.PaymentHystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p></p>
 *
 * @author WangJialu
 * @since 2021/6/23 18:53
 */
@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "defaultFallback")
public class OrderHyrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String res = paymentHystrixService.paymentInfo_OK(id);
        System.out.println(res);
        return res;
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }

    @GetMapping("/payment/hystrix/error/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler")
    public String paymentInfo_Error(@PathVariable("id") Integer id) {
        int age = 10 / 0;
        return paymentHystrixService.paymentInfo_Error(id);
    }

    public String paymentInfo_ERROR_Handler(Integer id) {
        return "consumer友好提示____paymentInfo_ERROR_Handler__/(ㄒoㄒ)/~~___" + id + "异常返回";
    }

    @GetMapping("/defaultMethod/{id}")
    @HystrixCommand
    public String defaultMethod(@PathVariable("id") Integer id) {
        int age = 10 / 0;
        return paymentHystrixService.paymentInfo_Error(id);
    }

    public String defaultFallback() {
        return "我是全局默认Fallback方法";
    }

    @GetMapping("/payment/hystrix/error2/{id}")
    public String paymentInfo_Error2(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_Error2(id);
    }
}
